package com.csun.cmny.python;

import java.io.*;
import java.net.Socket;
import java.util.Date;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-04-09 09:49
 * @Description:
 */
public class CsunPythonSocketMain {

    public static void main(String[] args) {

        String ip = "192.168.1.100";
        int port = 17000;

        //String inputJsonFilePath = args[0];
        //String inputJsonFilePath = "G:\\python\\20190408172336309input.json";
//        String inputJsonFilePath = "G:\\python\\serverClient\\20190328112128590input.json";
//        String inputJsonFilePath = "G:\\python\\serverClient\\tmp.json";
        String inputJsonFilePath = "G:\\python\\20190408172752636input.json";

        try {

            Date start = new Date();
            Socket socket = new Socket(ip, port);
//
            // 读取文件
            String line;
            String inputJson = "";
            StringBuilder sb = new StringBuilder();
            FileReader fileReader = new FileReader(inputJsonFilePath);
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            while ((line = bufferedReader.readLine()) != null) {
                // 一次读入一行数据
                sb.append(line);
            }
            inputJson = sb.toString();
            System.out.println("inputJson: = " + inputJson);

            //String tmpJson = "{\"breath\":[10,11],\"heartbeat\":[82,83],\"inBed\":[1,1],\"start\":1553328830,\"createdTime\":[1553328830,1553328841],\"end\":1553328841,\"step\":1,\"status\":[\"online\",\"online\"],\"smoothFactor\":300}";
            //System.out.println("tmpJson = " + tmpJson);

            //String tmpJson = "{\"breath\":[10],\"actionTime\":[1553328830],\"heartbeat\":[82],\"inBed\":[1],\"start\":1553328830,\"createdTime\":[1553328830],\"end\":1553328830,\"action\":[1],\"step\":1,\"status\":[\"online\"],\"smoothFactor\":300}";
            //System.out.println("tmpjson = " + tmpJson);

            //String tmpJson = "{\"breath\":[-1,8,10,13,12,14,9,15,18,10,10,5,10,10,-1,-1,10,10,10,9,3,13,18,14,13,10,8,10,17,5,12,13,7,4,-1,-1,10,10,10,15,10,15,14,10,10,10,10,10,-1,-1,-1,-1,10,10,11,11,15,16,-1,-1,-1,-1,14,14,4,11,8,17,9,10,8,17,11,11,14,-1,-1,-1,-1,9,15,10,10,10,21,13,13],\"actionTime\":[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null],\"heartbeat\":[-1,78,75,80,64,76,77,70,83,81,82,80,79,73,-1,-1,73,80,80,73,60,76,86,37,80,64,83,73,65,51,57,77,59,70,-1,-1,64,79,80,76,64,86,84,54,59,91,57,63,-1,-1,-1,-1,83,74,65,80,64,81,-1,-1,-1,-1,79,80,73,75,79,87,76,79,74,62,56,64,74,-1,-1,-1,-1,75,91,79,79,33,83,74,76],\"inBed\":[0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,0,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1],\"smoothFactor\":300,\"start\":1554269400,\"createdTime\":[1554269441,1554269450,1554269470,1554269490,1554269510,1554269530,1554269550,1554269570,1554269590,1554269610,1554269630,1554269650,1554269670,1554269690,1554269744,1554269748,1554269766,1554269786,1554269806,1554269826,1554269846,1554269866,1554269886,1554269906,1554269926,1554269946,1554269966,1554269986,1554270006,1554270026,1554270046,1554270066,1554270085,1554270106,1554270111,1554270141,1554270160,1554270181,1554270200,1554270221,1554270241,1554270260,1554270280,1554270302,1554270321,1554270340,1554270360,1554270380,1554270391,1554270402,1554270406,1554270461,1554270480,1554270500,1554270520,1554270541,1554270560,1554270580,1554270585,1554270630,1554270687,1554270691,1554270709,1554270729,1554270749,1554270769,1554270789,1554270809,1554270829,1554270849,1554270869,1554270889,1554270909,1554270929,1554270949,1554270965,1554271009,1554271030,1554271034,1554271052,1554271072,1554271092,1554271112,1554271132,1554271152,1554271172,1554271192],\"action\":[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],\"end\":1554271200,\"step\":10,\"status\":[\"offline\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"offline\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"offline\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\",\"online\"]}";

            String tmpJson = "{\"breath\":[-1,8,10],\"actionTime\":[null,null,null],\"heartbeat\":[-1,78,75,80],\"inBed\":[0,1,1],\"smoothFactor\":300,\"start\":1554269441,\"createdTime\":[1554269441,1554269450,1554269470],\"action\":[1,1,1],\"end\":1554269470,\"step\":10,\"status\":[\"offline\",\"online\",\"online\"]}";

            BufferedWriter write = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));     //可用PrintWriter
            //PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            String hello = in.readLine();
            System.out.println(hello);
            if (!hello.equals("Hello")) {
                return ;
            }

//            write.write("20190328112128590input.json");
            String writeData = inputJson + "CSUN";
            System.out.println("write data = " + writeData);
            write.write(writeData);
            write.flush();

//            StringBuilder sb = new StringBuilder();
//            while ((line = in.readLine()) != null) {
//                sb.append(line);
//            }

            String outputJson = in.readLine();
            System.out.println("outputJson: = " + outputJson);

            Date end = new Date();
            System.out.println("time: " + (end.getTime() - start.getTime()));

            Thread.sleep(1000 * 60);

            socket.close();

        } catch (Exception e) {
            e.printStackTrace();
//            System.out.println("error: " + e.getMessage());
        }

    }
}
